package com.bytedance.service.impl;

import com.bytedance.dao.ADAO;
import com.bytedance.dao.BDAO;
import com.bytedance.dataobject.A;
import com.bytedance.dataobject.B;
import com.bytedance.model.Result;
import com.bytedance.service.BService;
import com.bytedance.util.ClassExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BServiceImpl implements BService {

    @Autowired
    private BDAO bdao;

    @Autowired
    private ADAO adao;

    @Override
    public Result<B> add(B b) {
        Result<B> result = new Result<>();
        B getB = bdao.getBById(b);
        if (getB != null) {
            result.setResultFailed("该对象B已存在！");
            return result;
        }
        A getA = adao.getAById(b.getA());
        if (getA == null) {
            result.setResultFailed("该对象B对应的对象A不存在！");
            return result;
        }
        setCorrect(getA, b);
        bdao.addB(b);
        result.setResultSuccess("添加对象B成功！", b);
        return result;
    }

    @Override
    public Result<B> update(B b) throws Exception {
        Result<B> result = new Result<>();
        B getB = bdao.getBById(b);
        if (getB == null) {
            result.setResultFailed("该对象B不存在！");
            return result;
        }
        A getA = adao.getAById(b.getA());
        if (getA == null) {
            result.setResultFailed("该对象B对应的对象A不存在！");
            return result;
        }
        ClassExamine.objectOverlap(b, getB);
        setCorrect(getA, b);
        bdao.updateB(b);
        result.setResultSuccess("更新对象B成功！", b);
        return result;
    }

    @Override
    public Result<List<B>> add(List<B> bList) {
        Result<List<B>> result = new Result<>();
        for (B b : bList) {
            B getB = bdao.getBById(b);
            if (getB != null) {
                result.setResultFailed("该对象B已存在！重复字段A:" + b.getA() + "，重复字段C:" + b.getC());
                return result;
            }
            A getA = adao.getAById(b.getA());
            if (getA == null) {
                result.setResultFailed("该对象B对应的对象A不存在！不存在字段A:" + b.getA());
                return result;
            }
            setCorrect(getA, b);
        }
        for (B b : bList) {
            bdao.addB(b);
        }
        //bdao.addBList(bList);
        result.setResultSuccess("批量添加对象B成功！", bList);
        return result;
    }

    @Override
    public Result<Integer> delete(String a, String c) {
        Result<Integer> result = new Result<>();
        int res = bdao.deleteBById(a,c);
        if (res == 0) {
            result.setResultFailed("删除对象B失败！不存在字段A为:" + a + "且字段C为:" + c + "的对象B");
            return result;
        }
        result.setResultSuccess("删除对象B成功！", 1);
        return result;
    }

    @Override
    public Result<List<B>> getAll() {
        Result<List<B>> result = new Result<>();
        List<B> bList = bdao.getBList();
        result.setResultSuccess("获取对象B列表成功！", bList);
        return result;
    }

    @Override
    public Result<Integer> deleteByC(String c) {
        Result<Integer> result = new Result<>();
        int res = bdao.deleteBByC(c);
        if (res == 0) {
            result.setResultFailed("删除对象B失败！不存在字段C为:" + c + "的对象B");
            return result;
        }
        result.setResultSuccess("删除对象B成功！", res );
        return result;
    }

    private void setCorrect(A a, B b) {
        b.setAaCorrect(a.getAa().equals(1) && b.getAa().equals(1));
        b.setBbCorrect(a.getBb().equals(1) && b.getBb().equals(1));
        b.setCcCorrect(a.getCc().equals(1) && b.getCc().equals(1));
        b.setDdCorrect(a.getDd().equals(1) && b.getDd().equals(1));
        b.setEeCorrect(a.getEe().equals(1) && b.getEe().equals(1));
    }
}
